package club.xyes.zkh.ecommerce.service.impl;

import club.xyes.zkh.ecommerce.commons.entity.Commodity;
import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.properties.WebsiteConfigurationProperties;
import club.xyes.zkh.ecommerce.commons.util.MultipartFileUtil;
import club.xyes.zkh.ecommerce.dao.mapper.CommodityMapper;
import club.xyes.zkh.ecommerce.service.CommodityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Create by 郭文梁 2019/4/17 0017 10:24
 * CommodityServiceImpl
 * 商品相关业务行为实现
 *
 * @author 郭文梁
 * @data 2019/4/17 0017
 */
@Service
public class CommodityServiceImpl extends BaseServiceImpl<Commodity> implements CommodityService {
    private final CommodityMapper mapper;
    private final WebsiteConfigurationProperties websiteConfigurationProperties;

    @Autowired
    public CommodityServiceImpl(CommodityMapper mapper,
                                WebsiteConfigurationProperties websiteConfigurationProperties) {
        super(mapper);
        this.mapper = mapper;
        this.websiteConfigurationProperties = websiteConfigurationProperties;
    }

    @Override
    public PageInfo<Commodity> findByClassificationId(Integer classificationId, Integer page, Integer rows) {
        Commodity query = new Commodity();
        query.setClassificationId(classificationId);
        return PageHelper.startPage(page, rows).doSelectPageInfo(() -> mapper.select(query));
    }

    @Override
    public Commodity create(Commodity commodity, MultipartFile frontCoverFile) throws IOException {
        String path = getCoverFilePath();
        String filename = MultipartFileUtil.save(frontCoverFile, path);
        commodity.setFrontCover(filename);
        return save(commodity);
    }

    @Override
    public Commodity setFrontCover(Commodity commodity, MultipartFile coverFile) throws IOException {
        String path = getCoverFilePath();
        String filename = MultipartFileUtil.save(coverFile, path);
        commodity.setFrontCover(filename);
        return updateById(commodity);
    }

    @Override
    public boolean existsByClassificationId(Integer classificationId) {
        Commodity query = new Commodity();
        query.setClassificationId(classificationId);
        return mapper.selectCount(query) > 0;
    }

    @Override
    public PageInfo<Commodity> findByShop(Shop shop, Integer page, Integer rows) {
        return PageHelper.startPage(page, rows).doSelectPageInfo(() -> mapper.selectByShopId(shop.getId()));
    }

    @Override
    public PageInfo<Commodity> searchByShopIdAndStateAndName(Integer shopId, Integer state, String name, Integer page, Integer rows) {
        String queryString = "%" + name + "%";
        return PageHelper.startPage(page, rows).doSelectPageInfo(() -> mapper.selectByShopIdAndLikeNameAndState(shopId, queryString, state));
    }

    /**
     * 封面文件保存路径
     *
     * @return 路径
     */
    private String getCoverFilePath() {
        return websiteConfigurationProperties.getStaticFilePath()
                + websiteConfigurationProperties.getCommodityFrontCoverPath();
    }
}
